<template>
    <div class="edu-page-warp">
        <inner-breadcrumb
            v-model="key"
            :label-args="{topTitle:this.topTitle, addTitle: this.addTitle, detailTitle: this.detailTitle}"
            :path="{
      label : '{topTitle}',
      key: 'list',
      children:[
        {
          key: 'add',
          label:'{addTitle}'
        },
         {
           key: 'detail',
          label:'{detailTitle}'
          }
        ]
    }"
        />
        <div class="edu-page-warp" v-show="key=='list'">
            <condition-card @reset="reset" @search="searchList" label-width="110px">
                <el-row>
                    <el-col :span="8">
                        <el-form-item :label="$t('Parameter.personnelCollenge')">
                            <DictSelect
                                v-model="queryParams.personnelCollege"
                                :data="options.faculty"
                                filterable
                            ></DictSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item :label="$t('Parameter.tutorCollenge')">
                            <DictSelect
                                v-model="queryParams.faculty"
                                :data="options.faculty"
                                filterable
                            ></DictSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item :label="$t('label.major')">
                            <MyMajor
                                :faculty="queryParams.faculty"
                                :grade="currentGrade"
                                v-model="queryParams.major"
                            ></MyMajor>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item :label="$t('Parameter.title')">
                            <DictSelect
                                v-model="queryParams.title"
                                :data="options.title"
                                filterable
                            ></DictSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item :label="$t('tutorList.status')">
                            <DictSelect v-model="queryParams.status" :data="options.status"></DictSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item :label="$t('tutorList.isAcrossMajor')">
                            <DictSelect v-model="queryParams.isAcross" :data="options.isAcross"></DictSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item :label="$t('tutorList.employmentYear')">
                            <SchoolYear
                                v-model="queryParams.grade"
                                :showAll="false"
                                :clearable="false"
                            ></SchoolYear>
                        </el-form-item>
                    </el-col>
                </el-row>
            </condition-card>
            <el-card shadow="never">
                <div class="edu-page-title">{{topTitle}}</div>
                <div class="edu-opertion-bar">
                    <div class="float-left">
                        <KeywordInput
                            v-model="queryParams.condition"
                            @enter="searchList"
                            placeholder="请输入工号或姓名搜索"
                        ></KeywordInput>
                    </div>
                    <div class="float-right">
                        <el-button
                            @click="exportTutor"
                            v-premit="export_tutor"
                        >{{$t('message.export')}}</el-button>
                        <el-dropdown @command="importRecord">
                            <el-button v-premit="import_tutor">
                                {{$t('message.import')}}
                                <i
                                    class="el-icon-arrow-down el-icon--right"
                                ></i>
                            </el-button>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item command="importIn">{{$t('message.import')}}</el-dropdown-item>
                                <el-dropdown-item command="importOut">{{$t('message.down')}}</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                        <el-dropdown @command="importRecord">
                            <el-button type="primary" v-premit="extend_tutor">
                                {{$t('tutorGuide.extendedImport')}}
                                <i
                                    class="el-icon-arrow-down el-icon--right"
                                ></i>
                            </el-button>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item command="renewalIn">{{$t('message.import')}}</el-dropdown-item>
                                <el-dropdown-item command="renewalOut">{{$t('message.down')}}</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                        <el-button
                            @click="updateRecord(null)"
                            v-premit="add_tutor"
                            type="primary"
                        >{{$t('common.btn.add')}}</el-button>
                    </div>
                </div>
                <edu-table
                    url="/api/welcomeservice/tutorList/list"
                    ref="mainTable"
                    :order="false"
                    :selection="true"
                >
                    <el-table-column :label="$t('Parameter.jobNumber')" prop="teacherCode" />
                    <el-table-column :label="$t('label.stuName')" prop="teacherName" />
                    <el-table-column :label="$t('label.sex')" prop="sexI18n" />
                    <el-table-column
                        :label="$t('Parameter.title')"
                        prop="titleI18n"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        :label="$t('Parameter.personnelCollenge')"
                        prop="personnelCollegeI18n"
                        :render-header="labelHead"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        :label="$t('Parameter.tutorCollenge')"
                        prop="facultyI18n"
                        :render-header="labelHead"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        :label="$t('label.major')"
                        prop="majorI18n"
                        show-overflow-tooltip
                    />
                    <el-table-column :label="$t('tutorList.planNumber')" prop="planNum">
                        <template slot-scope="scope">
                            <span>{{scope.row.planNum ? scope.row.planNum:0}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$t('tutorList.actualNumber')" prop="actNum">
                        <template slot-scope="scope">
                            <span>{{scope.row.actNum ? scope.row.actNum:0}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$t('tutorList.status')" prop="status">
                        <template slot-scope="scope">
                            <el-tag
                                v-if="scope.row.status == 1"
                                type="success"
                            >{{$t('tutorList.effective')}}</el-tag>
                            <el-tag v-else>{{$t('tutorList.invalid')}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column
                        :label="$t('message.remark')"
                        prop="remark"
                        show-overflow-tooltip
                    />
                    <el-table-column :label="$t('message.operating')" fixed="right" width="130">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                @click="updateRecord(scope.row)"
                                size="mini"
                                v-premit="edit_tutor"
                            >{{$t('message.edit')}}</el-button>
                            <el-button
                                type="text"
                                @click="detail(scope.row)"
                                size="mini"
                                v-premit="detail_tutor"
                            >{{$t('message.detail')}}</el-button>
                            <el-button
                                type="text"
                                @click="del(scope.row)"
                                size="mini"
                                v-premit="del_tutor"
                            >{{$t('message.delete')}}</el-button>
                        </template>
                    </el-table-column>
                </edu-table>
            </el-card>
        </div>
        <!-- 新增编辑 -->
        <div class="edu-page-warp" v-if="key == 'add'">
            <el-card shadow="never">
                <div class="edu-page-title">{{addTitle}}</div>
                <hr />
                <el-form
                    :model="createForm"
                    ref="createForm"
                    label-width="120px"
                    label-suffix=":"
                    :rules="rules"
                >
                    <el-row>
                        <el-col :span="4">
                            <el-upload
                                class="avatar-uploader"
                                action="/api/commonservice/obsfile/uploadfile"
                                :show-file-list="false"
                                :on-success="handleAvatarSuccess"
                                :data="{bucketname: 'face', folderpath: 'file'}"
                                :file-list="fileList"
                            >
                                <img v-if="createForm.photo" :src="imgUrl" class="avatar" />
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>
                        </el-col>
                        <el-col :span="20">
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item
                                        :label="$t('Parameter.jobNumber')"
                                        prop="teacherCode"
                                    >
                                        <el-input
                                            v-model="createForm.teacherCode"
                                            @keyup.enter.native="getTeacherfo"
                                            @change="getTeacherfo"
                                            :disabled="isMultiple"
                                        ></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item :label="$t('label.stuName')" prop="teacherName">
                                        <el-input v-model="createForm.teacherName" :disabled="true"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item :label="$t('label.sex')" prop="sex">
                                        <DictSelect
                                            v-model="createForm.sex"
                                            :data="options.sex"
                                            :disabled="true"
                                        ></DictSelect>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item :label="$t('Parameter.title')" prop="title">
                                        <DictSelect
                                            v-model="createForm.title"
                                            :data="options.title"
                                            :disabled="true"
                                        ></DictSelect>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item
                                        :label="$t('Parameter.personnelCollenge')"
                                        prop="personnelCollege"
                                    >
                                        <DictSelect
                                            v-model="createForm.personnelCollege"
                                            :data="options.faculty"
                                            :disabled="true"
                                        ></DictSelect>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item
                                        :label="$t('Parameter.tutorCollenge')"
                                        prop="faculty"
                                    >
                                        <DictSelect
                                            v-model="createForm.faculty"
                                            :data="options.faculty"
                                            :disabled="editFaculty || tutorEdit"
                                        ></DictSelect>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12" v-if="!isMultiple">
                                    <el-form-item :label="$t('label.major')" prop="major">
                                        <MyMajor
                                            :faculty="createForm.faculty"
                                            :grade="currentGrade"
                                            :multiple="true"
                                            @load-done="getAllMajor"
                                            v-model="createForm.majors"
                                        ></MyMajor>
                                    </el-form-item>
                                </el-col>
                                <!-- 编辑单选 -->
                                <el-col :span="12" v-else>
                                    <el-form-item :label="$t('label.major')" prop="major">
                                        <MyMajor
                                            :faculty="createForm.faculty"
                                            :grade="currentGrade"
                                            :disabled="tutorEdit"
                                            :clearable="false"
                                            v-model="createForm.major"
                                        ></MyMajor>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item :label="$t('tutorList.status')" prop="status">
                                        <DictSelect
                                            v-model="createForm.status"
                                            :data="options.status"
                                            :disabled="tutorEdit"
                                        ></DictSelect>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item
                                        :label="$t('tutorList.employmentYear')"
                                        prop="grade"
                                    >
                                        <SchoolYear
                                            v-model="createForm.grade"
                                            :showAll="false"
                                            :disabled="tutorEdit"
                                        ></SchoolYear>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item
                                        :label="$t('tutorList.officePhone')"
                                        prop="officePhone"
                                    >
                                        <el-input v-model="createForm.officePhone" maxlength="32"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item
                                        :label="$t('tutorList.telePhone')"
                                        prop="telPhone"
                                    >
                                        <el-input v-model="createForm.telPhone" maxlength="32"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item :label="$t('tutorList.email')" prop="email">
                                        <el-input v-model="createForm.email" maxlength="64"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="24">
                                    <el-form-item
                                        :label="$t('tutorList.tutorIntroduction')"
                                        prop="introduce"
                                    >
                                        <el-input
                                            type="textarea"
                                            :autosize="{ minRows: 2, maxRows: 4}"
                                            v-model="createForm.introduce"
                                            clearable
                                            maxlength="2048"
                                            show-word-limit
                                        ></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="24">
                                    <el-form-item :label="$t('message.remark')" prop="remark">
                                        <el-input
                                            type="textarea"
                                            :autosize="{ minRows: 2, maxRows: 4}"
                                            v-model="createForm.remark"
                                            clearable
                                            maxlength="255"
                                            show-word-limit
                                        ></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-col>
                    </el-row>
                </el-form>
                <div class="float-right">
                    <el-button @click="reback(createForm)">{{$t('message.return')}}</el-button>
                    <el-button type="primary" @click="submitForm">{{$t('message.save')}}</el-button>
                </div>
            </el-card>
        </div>
        <!-- 导师详情 -->
        <div class="edu-page-warp" v-if="key == 'detail'">
            <el-card shadow="never">
                <div class="edu-page-title">{{detailTitle}}</div>
                <hr />
                <el-form :model="detailForm" ref="detailForm" label-width="120px" label-suffix=":">
                    <el-row>
                        <el-col :span="4">
                            <div align="center" class="picture">
                                <img v-if="detailForm.photo" :src="imgUrl" class="avatar" />
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </div>
                        </el-col>
                        <el-col :span="20">
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item
                                        :label="$t('Parameter.jobNumber')"
                                        prop="teacherCode"
                                    >
                                        <span>{{detailForm.teacherCode}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item :label="$t('label.stuName')" prop="teacherName">
                                        <span>{{detailForm.teacherName}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item :label="$t('label.sex')" prop="sex">
                                        <span>{{detailForm.sexI18n}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item :label="$t('Parameter.title')" prop="title">
                                        <span>{{detailForm.titleI18n}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item
                                        :label="$t('Parameter.personnelCollenge')"
                                        prop="personnelCollege"
                                    >
                                        <span>{{detailForm.personnelCollegeI18n}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item
                                        :label="$t('Parameter.tutorCollenge')"
                                        prop="faculty"
                                    >
                                        <span>{{detailForm.facultyI18n}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item :label="$t('label.major')" prop="major">
                                        <span>{{detailForm.majorI18n}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item :label="$t('tutorList.status')" prop="status">
                                        <span
                                            v-if="detailForm.status == 1"
                                        >{{$t('tutorList.effective')}}</span>
                                        <span v-else>{{$t('tutorList.invalid')}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item
                                        :label="$t('tutorList.employmentYear')"
                                        prop="status"
                                    >
                                        <span>{{transGrade(detailForm.grade)}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item
                                        :label="$t('tutorList.officePhone')"
                                        prop="officePhone"
                                    >
                                        <span>{{detailForm.officePhone}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item
                                        :label="$t('tutorList.telePhone')"
                                        prop="telPhone"
                                    >
                                        <span>{{detailForm.telPhone}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item :label="$t('tutorList.email')" prop="email">
                                        <span>{{detailForm.email}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="24">
                                    <el-form-item
                                        :label="$t('tutorList.tutorIntroduction')"
                                        prop="introduce"
                                    >
                                        <span
                                            style=" width:100%;word-break:break-all;"
                                        >{{detailForm.introduce}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="24">
                                    <el-form-item :label="$t('message.remark')" prop="remark">
                                        <span
                                            style=" width:100%;word-break:break-all;"
                                        >{{detailForm.remark}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="24">
                                    <el-table
                                        :data="detailForm.grades"
                                        style="width: 50%;margin-left:10%;"
                                    >
                                        <el-table-column :label="$t('tutorList.employmentYear')">
                                            <template slot-scope="scope">
                                                <span>
                                                    {{transGrade(scope.row)}}
                                                    <el-button
                                                        type="warning"
                                                        size="mini"
                                                        round
                                                        v-if="scope.row == detailForm.grade"
                                                        style="margin-left:60%"
                                                    >NEW</el-button>
                                                </span>
                                            </template>
                                        </el-table-column>
                                    </el-table>
                                </el-col>
                            </el-row>
                        </el-col>
                    </el-row>
                </el-form>
                <div class="float-right">
                    <el-button @click="reback(null)">{{$t('message.return')}}</el-button>
                </div>
            </el-card>
        </div>

        <!-- 导入导师 -->
        <edu-dialog
            :title="$t('message.import')"
            size="small"
            ref="uploadDialog"
            @close="clearFiles"
            @confirm="uploadFile"
        >
            <el-form :model="createForm" ref="createForm" label-width="85px" v-loading="loading1">
                <el-row>
                    <el-form-item>
                        <el-upload
                            ref="upload"
                            :data="{}"
                            :on-success="uploadDone"
                            :auto-upload="false"
                            :multiple="false"
                            :on-change="(file, fileList)=>{this.importFileList = fileList}"
                            :limit="1"
                            :accept="'application/vnd.ms-excel'"
                            :action="uploadUrl"
                        >
                            <el-button
                                slot="trigger"
                                size="small"
                                type="primary"
                            >{{$t('label.selectFile')}}</el-button>
                            <div
                                slot="tip"
                                class="el-upload__tip"
                            >{{$t('courseTake.uploadXLSFileOnly')}}</div>
                        </el-upload>
                    </el-form-item>
                </el-row>
            </el-form>
        </edu-dialog>
        <el-dialog title="系统提示" :visible.sync="dialogVisible" width="40%">
            <span>返回将不会保存当前信息激活设置，是否确定？</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancle">取 消</el-button>
                <el-button type="primary" @click="summit">确 定</el-button>
                <el-button type="primary" @click="saveClose">保存并关闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { queryDic } from 'common/src/api/dictionary'
import APIs from 'common/src/api/welcomeService/tutorList'
import SchoolYear from './SchoolYear'
import SchoolMajorSelect from './SchoolMajorSelect'
import MyMajor from './MyMajor'
import UTILS from 'common/src/utils/utils'
import CPIs from 'common/src/api/welcomeService/tutorExportImport'
import { mapState } from 'vuex'

const defaultCreateForm = {
    teacherCode: '',
    teacherName: '',
    sex: '',
    personnelCollege: '',
    faculty: '',
    majors: [],
    title: '',
    status: 1,
    officePhone: '',
    telePhone: '',
    email: '',
    introduction: '',
    remark: '',
    photo: '',
    grade: null,
    major: ''
}
// 本科生导师库
export default {
    name: 'TutorListCollege',
    computed: {
        ...mapState({
            currentRole: state => {
                let mockUser = state.session.mockUser
                let user = state.session.user
                if (mockUser) {
                    if (mockUser.currentRole == 2) {
                        return true
                    } else {
                        return false
                    }
                } else {
                    if (user.currentRole == 2) {
                        return true
                    } else {
                        return false
                    }
                }
            }
        })
    },
    components: {
        SchoolYear,
        SchoolMajorSelect,
        MyMajor
    },
    mounted() {
        //获取当前年份
        this.currentGrade = new Date().getFullYear() - 1
        this.getOptions()
    },
    data() {
        return {
            tutorType: '2',
            currentGrade: '',
            key: 'list',
            topTitle: '',
            addTitle: '',
            detailTitle: '',
            dialogVisible: false,
            loading1: false,
            tutorEdit: false,
            defaultPhoto: '/static/images/icon_photo.png',
            api: CPIs,
            uploadUrl: '',
            message: '',
            isMultiple: false,
            editFaculty: false,
            imgUrl: '',
            options: {
                faculty: [],
                status: [
                    { label: this.$t('tutorList.invalid'), value: 0 },
                    { label: this.$t('tutorList.effective'), value: 1 }
                ],
                title: [],
                isAcross: [
                    { label: this.$t('message.no'), value: 0 },
                    { label: this.$t('message.yes'), value: 1 }
                ],
                sex: []
            },
            queryParams: {
                personnelCollege: '',
                faculty: '',
                major: '',
                title: '',
                status: '',
                isAcross: '',
                condition: '',
                type: '',
                grade: null
            },
            collegeMajor: [],
            rules: {
                teacherCode: [
                    {
                        required: true,
                        validator: this.checkTeacherCode,
                        trigger: 'blur'
                    }
                ],
                faculty: [
                    {
                        required: true,
                        message: this.$t('message.inputRequired'),
                        trigger: 'blur'
                    }
                ],
                grade: [
                    {
                        required: true,
                        message: this.$t('message.inputRequired'),
                        trigger: 'blur'
                    }
                ],
                status: [
                    {
                        required: true,
                        message: this.$t('message.inputRequired'),
                        trigger: 'blur'
                    }
                ]
            },
            fileList: [],
            importFileList: [],
            createForm: UTILS.clone(defaultCreateForm),
            detailForm: UTILS.clone(defaultCreateForm)
        }
    },
    methods: {
        getOptions() {
            //筛选条件字典查询
            let data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: ['X_YX', 'profession', 'G_XBIE']
            }
            queryDic(data).then(res => {
                UTILS.fillSelect(
                    {
                        faculty: 'X_YX',
                        title: 'profession',
                        sex: 'G_XBIE'
                    },
                    res.data,
                    this.options
                )
                this.topTitle = '学院导师库'

                this.searchList()
            })
        },
        transGrade(data) {
            if (data) {
                return data + '-' + (data + 1) + '年度'
            } else {
                return ''
            }
        },
        reback(data) {
            if (!data) {
                this.$nextTick(() => {
                    this.resetAddForm()
                })
                this.key = 'list'
            } else {
                if (data.id) {
                    this.$nextTick(() => {
                        this.resetAddForm()
                    })
                    this.key = 'list'
                } else {
                    this.dialogVisible = true
                }
            }
        },
        cancle() {
            this.dialogVisible = false
        },
        summit() {
            this.dialogVisible = false
            this.resetAddForm()
            this.key = 'list'
        },
        saveClose() {
            this.dialogVisible = false
            this.submitForm()
        },
        detail(item) {
            this.key = 'detail'
            this.imgUrl = ''
            this.detailTitle = '学院导师' + this.$t('message.detail')
            APIs.findTutorById(item.id).then(res => {
                if (res.code == 200) {
                    this.detailForm = res.data
                    if (this.detailForm.photo) {
                        this.findTutorPicture(this.detailForm.photo)
                    } else {
                        this.imgUrl = this.defaultPhoto
                    }
                }
            })
        },
        reset() {
            for (const key in this.queryParams) {
                this.queryParams[key] = ''
            }
            this.searchList()
        },
        handleAvatarSuccess(res, file, fileList) {
            this.imgUrl = URL.createObjectURL(file.raw)
            this.createForm.photo = ''
            let name = file.name
            if (res.code == 200) {
                this.createForm.photo = res.data[name].objectkey
            }
        },
        exportTutor() {
            let loading = this.$loading({
                lock: true,
                text: 'Loading',
                spinner: 'el-icon-loading',
                background: 'rgba(255, 255, 255, .48)'
            })
            this.queryParams.type = this.tutorType
            let data = _.cloneDeep(this.queryParams)
            data.ids = this.$refs.mainTable.getCheckData().map(c => c.id)
            CPIs.exportTutor(data).then(res => {
                if (res.code == 200) {
                    this.delayRequest(res.data.key, loading)
                } else {
                    loading.close()
                }
            })
        },
        async delayRequest(key, loading) {
            await new Promise(resolve => setTimeout(resolve, 3000))
            CPIs.getReportStatus(key)
                .then(res => {
                    if (res.code == 200 && res.data.status) {
                        let path = CPIs.exportLogUrl + res.data.path
                        window.location.href = path
                        loading.close()
                    } else {
                        this.delayRequest(key, loading)
                    }
                })
                .catch(ex => {
                    this.delayRequest(key, loading)
                })
        },
        importRecord(type) {
            this.uploadUrl = ''
            if (type == 'importIn') {
                //导入
                this.uploadUrl = '/api/welcomeservice/tutorList/import/tutor/' + this.tutorType
                this.$refs.uploadDialog.open()
            } else if (type == 'importOut') {
                //下载导入模板
                window.location.href = CPIs.tutorTemplate(this.tutorType)
            } else if (type == 'renewalOut') {
                //下载续期导入模板
                window.location.href = CPIs.gradeTemplate(this.tutorType)
            } else if (type == 'renewalIn') {
                this.uploadUrl = '/api/welcomeservice/tutorList/import/grade/' + this.tutorType
                this.$refs.uploadDialog.open()
            }
        },
        clearFiles() {
            this.$refs.upload.clearFiles()
            this.importFileList = []
        },
        uploadFile() {
            if (_.isEmpty(this.importFileList)) {
                this.$message({
                    type: 'info',
                    message: this.$t('message.selectFilePlease')
                })
                return
            }
            this.$refs.upload.submit()
        },
        uploadDone(res, file, importFileList) {
            if (res.code == 200) {
                this.loading1 = true
                this.delayImportRequest(res.data.key)
            } else {
                this.$message({
                    type: 'error',
                    message: this.$t('common.tips.dataFormatError')
                })
                this.clearFiles()
            }
        },
        async delayImportRequest(key) {
            await new Promise(resolve => setTimeout(resolve, 3000))
            CPIs.getImportStatus(key)
                .then(res => {
                    if (res.code == 200 && res.data.status) {
                        this.loading1 = false
                        this.clearFiles()
                        if (res.data.success) {
                            this.$message({
                                type: 'success',
                                message: '导入成功'
                            })
                            this.$refs.uploadDialog.close()
                            this.searchList()
                        } else {
                            this.$message({
                                type: 'error',
                                message: res.data.msg ? res.data.msg : '导入失败'
                            })
                        }
                    } else {
                        this.delayImportRequest(key)
                    }
                })
                .catch(ex => {
                    this.delayImportRequest(key)
                })
        },
        checkTeacherCode(rule, value, callback) {
            if (!value) {
                callback(new Error(this.$t('message.inputRequired')))
            } else if (!this.createForm.teacherName) {
                callback(new Error(this.$t('Parameter.teacherNotExist')))
            } else {
                callback()
            }
        },
        getTeacherfo() {
            let code = this.createForm.teacherCode
            if (!code) {
                this.createForm = UTILS.clone(defaultCreateForm)
                return
            }
            //查找教师信息
            APIs.findTeacherByCode(code).then(res => {
                if (res.code == 200 && res.data) {
                    this.createForm.teacherCode = res.data.code
                    this.createForm.teacherName = res.data.name
                    this.createForm.sex = res.data.sex
                    this.createForm.title = res.data.title
                    this.createForm.personnelCollege = res.data.faculty
                    this.createForm.photo = res.data.photo
                    if (this.createForm.photo) {
                        this.findTutorPicture(this.createForm.photo)
                    }
                    this.$refs.createForm.clearValidate()
                } else {
                    this.createForm = UTILS.clone(defaultCreateForm)
                    this.createForm.teacherCode = code
                    this.$refs.createForm.validateField('teacherCode')
                }
            })
        },
        updateRecord(data) {
            this.imgUrl = ''
            this.key = 'add'
            if (!data) {
                //新增
                this.editFaculty = false
                this.tutorEdit = false
                this.isMultiple = false //多选
                this.addTitle = this.$t('message.add') + '学院导师'
                this.createForm = UTILS.clone(defaultCreateForm)
            } else {
                if (this.currentRole) {
                    //是导师
                    this.tutorEdit = true
                } else {
                    this.tutorEdit = false
                }
                this.isMultiple = true //单选
                this.addTitle = this.$t('message.edit') + '学院导师'
                APIs.findTutorById(data.id).then(res => {
                    if (res.code == 200) {
                        this.createForm = _.cloneDeep(res.data)
                        if (this.createForm.actNum != 0) {
                            //有人，不让换导师所属学院
                            this.editFaculty = true
                        } else {
                            this.editFaculty = false
                        }
                        if (this.createForm.photo) {
                            this.findTutorPicture(this.createForm.photo)
                        }
                        let that = this
                        setTimeout(() => {
                            that.createForm.major = res.data.major
                        }, 500)
                    }
                })
            }
        },
        findTutorPicture(url) {
            APIs.findPicture(url).then(res => {
                if (res.code == 200 && res.data) {
                    this.imgUrl = res.data.base
                } else {
                    this.imgUrl = this.defaultPhoto
                }
            })
        },
        getAllMajor(data) {
            this.collegeMajor = []
            if (UTILS.isNotEmpty(data)) {
                _.forEach(data, d => {
                    this.collegeMajor.push(d.value)
                })
            }
        },
        searchList() {
            this.$nextTick(() => {
                this.queryParams.type = this.tutorType
                this.$refs.mainTable.search(this.queryParams)
            })
        },
        resetAddForm() {
            this.searchList()
            this.$refs.createForm.clearValidate()
            this.createForm = UTILS.clone(defaultCreateForm)
        },
        submitForm() {
            this.$refs.createForm.validate(valid => {
                if (valid) {
                    let item = UTILS.clone(this.createForm)
                    item.type = this.tutorType
                    if (item.id) {
                        //编辑
                        if (!item.major) {
                            this.$message({
                                type: 'warning',
                                message: '请选择专业'
                            })
                            return
                        }
                        APIs.editTutor(item).then(response => {
                            if (response.code == 200) {
                                let message = this.$t('message.editSuccess')
                                this.$message({
                                    type: 'success',
                                    message: message
                                })
                                this.reback(null)
                            }
                        })
                    } else {
                        //新增
                        if (UTILS.isEmpty(item.majors)) {
                            item.major = this.collegeMajor.join(',')
                        } else {
                            item.major = item.majors.join(',')
                        }
                        if (!item.major) {
                            this.$message({
                                type: 'warning',
                                message: '请选择专业'
                            })
                            return
                        }
                        APIs.addTutor(item).then(response => {
                            if (response.code == 200) {
                                let message = this.$t('message.addSuccess')
                                this.$message({
                                    type: 'success',
                                    message: message
                                })
                                this.reback(null)
                            }
                        })
                    }
                }
            })
        },
        labelHead(h, { column, $index }) {
            return h('span', { attrs: { title: column.label } }, [column.label])
        },
        del(val) {
            let data = {}
            data.ids = [val.id]
            this.$delConfirm().then(() => {
                APIs.del(data).then(res => {
                    if ((res.code = 200)) {
                        this.$message({
                            type: 'success',
                            message: this.$t('common.tips.deleteSuccess')
                        })
                        this.searchList()
                    }
                })
            })
        }
    }
}
</script>
<style lang="scss" >
.stuTitle {
    line-height: 40px;
    width: 100%;
    border: 1px solid #cce7ff;
    background-color: #eef7ff;
    padding-left: 30px;
}
.picture {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>
